package com.yjzx.util.rabbit.service.impl;

import com.rabbitmq.client.Channel;
import com.yjzx.util.common.util.encrypt.MD5;
import com.yjzx.util.rabbit.constant.RedisConst;
import com.yjzx.util.rabbit.service.RabbitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.io.IOException;
/**
 * @author yjzx
 * @date 2024/08/17
 * @description 失败手动调研此接口
 */
@Slf4j
public class RabbitServiceImpl implements RabbitService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 消息重试
     * @param channel
     * @param deliveryTag
     * @param msgContent
     * @param count
     * @throws IOException
     */
    @Override
    public void retry(Channel channel, Long deliveryTag, String msgContent, int count) throws IOException {
        Long increment = this.redisTemplate.opsForValue().increment(RedisConst.MQ_RETRY + MD5.encrypt(msgContent));
        if (increment<=count){
            log.error("第{}次，消息{}未成功消费",count,msgContent);
            channel.basicNack(deliveryTag,false,true);
        }else {
            log.error("消息{}多次无法消费，等待人工处理",msgContent);
        }
    }
}
